package com.future.jdkProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * Description:
 *
 * @author weiruibai.vendor
 * Date: 2021/9/22 09:36
 */
public class CalculatorProxy {
    public static Calculator getProxy(final Calculator calculator) {
        ClassLoader loader = calculator.getClass().getClassLoader();
        Class<?>[] interfaces = calculator.getClass().getInterfaces();
        InvocationHandler h = (proxy, method, args) -> {
            Object result = null;
            try {
                result = method.invoke(calculator, args);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {

            }
            return result;
        };
        Object proxy = Proxy.newProxyInstance(loader, interfaces, h);
        return (Calculator) proxy;
    }
}
